﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LuaInterface;
using NTUTMessagerAPI;

namespace NTUTMessager
{
    public partial class LoginForm : Form
    {
        SocketAPI _socketAPI;

        public LoginForm(SocketAPI socketAPI)
        {
            _socketAPI = socketAPI;

            InitializeComponent();

            _socketAPI.GetCommandEvent += getCommand;
        }

        public void getCommand(ConnectPair pair, String command) 
        {
            Lua lua = new Lua();

            lua.DoString(command);

            LuaTable loginResult = lua.GetTable("loginResult");

            if (loginResult != null) 
            {
                if ((Boolean)loginResult["result"])
                {
                   // MessageBox.Show("登入成功");

                    _socketAPI.GetCommandEvent = null;

                    DialogResult = System.Windows.Forms.DialogResult.OK;
                    
                }
                else 
                {
                    MessageBox.Show("帳號或密碼錯誤");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String account = textBox1.Text;
            String password = textBox2.Text;
            String publicKey = Program.MyPublicKey;

            _socketAPI.SendCommand(0, "loginTable = { [\"account\"] = \"" + account + "\",  [\"password\"] = \"" + password + "\",  [\"publicKey\"] = \"" + publicKey + "\",[\"ip\"] = \""+_socketAPI.MyIp+"\",}");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {

        }
    }
}
